package com.sys.ai.controller;

import com.sys.ai.domain.AiSendHistory;
import com.sys.ai.fo.UpdateGroupNameByCodeFO;
import com.sys.ai.service.IAiSendHistoryService;
import com.sys.ai.vo.MessageHistoryVO;
import com.sys.common.annotation.Log;
import com.sys.common.core.controller.BaseController;
import com.sys.common.core.domain.AjaxResult;
import com.sys.common.core.page.TableDataInfo;
import com.sys.common.enums.BusinessType;
import com.sys.common.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 发送记录Controller
 *
 * @author LGY
 * @date 2025-05-09
 */
@Tag(name = "发送记录")
@RestController
@RequestMapping("/tool/aiSendHistory")
public class AiSendHistoryController extends BaseController {
    @Autowired
    private IAiSendHistoryService aiSendHistoryService;

    /**
     * 查询发送记录列表
     */
    @PostMapping("/list")
    @Operation(summary = "查询发送记录分页列表")
    public TableDataInfo list(@RequestBody AiSendHistory aiSendHistory) {
        startPage(aiSendHistory);
        List<AiSendHistory> list = aiSendHistoryService.selectAiSendHistoryList(aiSendHistory);
        return getDataTable(list);
    }

    /**
     * 查询发送记录列表
     */
    @PostMapping("/listAll")
    @Operation(summary = "查询所有我的发送记录")
    public AjaxResult listAll(@RequestBody AiSendHistory aiSendHistory) {
        List<MessageHistoryVO> list = aiSendHistoryService.listAllMySelf(aiSendHistory.getAppId());
        return AjaxResult.success(list);
    }

    @GetMapping("/listByGroupCode/{groupCode}")
    @Operation(summary = "查询指定群组的发送记录")
    public AjaxResult listByGroupCode(@PathVariable("groupCode") String groupCode) {
        List<AiSendHistory> list = aiSendHistoryService.findAllByGroupCode(groupCode);
        return AjaxResult.success(list);
    }

    @DeleteMapping("/deleteByCode/{groupCode}")
    @Operation(summary = "删除指定群组的发送记录")
    public AjaxResult deleteByCode(@PathVariable("groupCode") String groupCode) {
        aiSendHistoryService.deleteByCode(groupCode);
        return success();
    }

    @PutMapping("/updateGroupNameByCode")
    @Operation(summary = "更新群组名称")
    public AjaxResult updateGroupNameByCode(@RequestBody UpdateGroupNameByCodeFO updateGroupNameByCodeFO) {
        aiSendHistoryService.updateGroupNameByCode(updateGroupNameByCodeFO);
        return success();
    }


    /**
     * 导出发送记录列表
     */
    @Log(title = "发送记录", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出发送记录")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AiSendHistory aiSendHistory) {
        List<AiSendHistory> list = aiSendHistoryService.selectAiSendHistoryList(aiSendHistory);
        ExcelUtil<AiSendHistory> util = new ExcelUtil<AiSendHistory>(AiSendHistory.class);
        util.exportExcel(response, list, "发送记录数据");
    }

    /**
     * 获取发送记录详细信息
     */
    @Operation(summary = "查询发送记录详情")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(aiSendHistoryService.selectAiSendHistoryById(id));
    }

    /**
     * 新增发送记录
     */
    @Log(title = "发送记录", businessType = BusinessType.INSERT)
    @Operation(summary = "新增发送记录")
    @PostMapping("/save")
    public AjaxResult add(@RequestBody AiSendHistory aiSendHistory) {
        aiSendHistoryService.insertAiSendHistory(aiSendHistory);
        return success();
    }

    /**
     * 修改发送记录
     */
    @Log(title = "发送记录", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改发送记录")
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody AiSendHistory aiSendHistory) {
        aiSendHistoryService.updateAiSendHistory(aiSendHistory);
        return success();
    }

    /**
     * 删除发送记录
     */
    @Operation(summary = "删除发送记录")
    @Log(title = "发送记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        aiSendHistoryService.deleteAiSendHistoryByIds(ids);
        return success();
    }
}